package com.yzh.oop9.oop9;

public class Test {
    public int constInt=5;

    public static void main(String[] args) {
        Book book1 = new Book("sdyxzh", "123金庸", "清华大学出版社", 34.7);
        Book book2 = new Book("bfmnzh", "321梁羽生", "清华大学出版社", 34.1);
        System.out.println(book1.compareTo(book2));

        //局部内部类
        class BookNameComparator implements Comparator {
            @Override
            public int compare(Object obj1, Object obj2) {
                Book book1 = (Book) obj1;
                Book book2 = (Book) obj2;
                return book1.getBookName().compareTo(book2.getBookName());
            }
        }

        //使用外部比较器比较图书
        Comparator comp1 = new BookNameComparator();
        int n = comp1.compare(book1, book2);
        System.out.println(n);


        class BookPublisherAuthorComparator implements Comparator {
            @Override
            public int compare(Object obj1, Object obj2) {
                Book book1 = (Book) obj1;
                Book book2 = (Book) obj2;
                int result = book1.getPublisher().compareTo(book2.getPublisher());
                if (result != 0) {
                    return result;
                } else {
                    //如果出版社相同，比较作者
                    return book1.getAuthor().compareTo(book2.getAuthor());
                }
            }
        }
        //使用外部比较器比较图书
        Comparator comp2 = new BookPublisherAuthorComparator();
        int n2 = comp2.compare(book1, book2);
        System.out.println(n2);
    }
}
